package com.shenda.device.codec;

import com.shenda.device.Device;
import com.shenda.device.Devices;
import com.shenda.device.message.ReqMsg;
import com.shenda.log.LogHolder;
import com.swak.rxtx.channel.Channel;
import com.swak.rxtx.channel.ChannelHandler;

/**
 * 命令解析
 * 
 * @author lifeng
 */
public class CommandEncoder extends ChannelHandler {

	@Override
	public void write(Channel channel, Object data) {
		if (data instanceof ReqMsg) {

			// 请求命令
			ReqMsg command = ((ReqMsg) data);

			// 对应设备
			Device device = Devices.me().getChannel(channel);
			if (device != null) {
				LogHolder.log(command.setDevice(device));
			}

			super.write(channel, ((ReqMsg) data).encode());
		} else {
			super.write(channel, data);
		}
	}
}
